//
//  JSBaseDataService.m
//  NiubilityProject
//
//  Created by Apple on 2018/3/6.
//  Copyright © 2018年 Apple. All rights reserved.
//

#import "JSBaseDataService.h"

@implementation JSBaseDataService

//MARK: 重新请求数据
-(void)refrenshData{
    self.page = 1;
    self.params = [self.delegate dataServiceParameter];
    self.params[@"page"] = @(self.page);
    NSInteger pageNumber = 0;
    if ([self.delegate respondsToSelector:@selector(dataServicePageNumber)]) {
        pageNumber = [self.delegate dataServicePageNumber]?:10;
    }
    self.params[@"pageNumber"] = @(pageNumber);
    if ([self.delegate respondsToSelector:@selector(overFooterRefrensh:)]) {
        [self.delegate overFooterRefrensh:YES];
    }
    if (![[self.delegate dataServiceUrlSting] length]) {
        JSLog(@"********************\n方法名为空 >>>>%s<<<<<<\n***************************", __func__);
        return;
    }
    if (![self.delegate dataServiceParameter]) {
        self.params = @{}.mutableCopy;
    }
    [JSNetManager js_requestWithType:JSHttpRequestTypePost urlString:[self.delegate dataServiceUrlSting] parameters:self.params successBlock:^(id response) {
        if ([self.delegate respondsToSelector:@selector(overHeaderRefrensh)]) {
            [self.delegate overHeaderRefrensh];
        }
        if ([response[kstatuStrKey] integerValue] == ksuccessCode) {
            [self.delegate dataServiceDidUpdate:self data:response error:nil];
        }else{
            NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:[response[kstatuStrKey] integerValue] userInfo:response];
            [self.delegate dataServiceDidUpdate:self data:nil error:error];
        }
    } failureBlock:^(NSError *error) {
        self.page --;
        if ([self.delegate respondsToSelector:@selector(overHeaderRefrensh)]) {
            [self.delegate overHeaderRefrensh];
        }
    }];
}
//MARK: 请求下一页数据
-(void)nextData{
    self.page ++;
    if ([self.delegate respondsToSelector:@selector(overHeaderRefrensh)]) {
        [self.delegate overHeaderRefrensh];
    }
    self.params = [self.delegate dataServiceParameter];
    self.params[@"page"] = @(self.page);
    NSInteger pageNumber = 0;
    if ([self.delegate respondsToSelector:@selector(dataServicePageNumber)]) {
        pageNumber = [self.delegate dataServicePageNumber]?:10;
    }
    self.params[@"pageNumber"] = @(pageNumber);
    if (![[self.delegate dataServiceUrlSting] length]) {
        JSLog(@"********************\n方法名为空 >>>>%s<<<<<<\n***************************", __func__);
        return;
    }
    if (![self.delegate dataServiceParameter]) {
        self.params = @{}.mutableCopy;
    }
    [JSNetManager js_requestWithType:JSHttpRequestTypePost urlString:[self.delegate dataServiceUrlSting] parameters:self.params successBlock:^(id response) {
        if ([self.delegate respondsToSelector:@selector(overFooterRefrensh:)]) {
            [self.delegate overFooterRefrensh:NO];
        }
        if ([response[kstatuStrKey] integerValue] == ksuccessCode) {
            [self.delegate dataServiceDidUpdate:self data:response error:nil];
        }else{
            NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:[response[kstatuStrKey] integerValue] userInfo:response];
            [self.delegate dataServiceDidUpdate:self data:nil error:error];
        }
    } failureBlock:^(NSError *error) {
        self.page --;
        if ([self.delegate respondsToSelector:@selector(overFooterRefrensh:)]) {
            [self.delegate overFooterRefrensh:NO];
        }
    }];
}

@end
